package com.xing.designmode;

import java.util.List;
import android.content.Context;
import android.graphics.Color;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.xing.controller.DBController;
import com.xing.model.HsdDataResult;
import com.xing.model.HsdModelAnswer;
import com.xing.model.HsdModelQuestion;
import com.xing.questionnairesurvey.R;

/**
 * 每行单选题
 * @author Administrator
 *
 */
public class SingleChoiceEachRow extends QuestionAnswer{

	private Handler handler1 = new Handler(){
		public void handleMessage(Message msg) {
			
		}
	};
	
	@Override
	public void showAnswer() {
		
		
	}
	
	public void showAnswer(HsdModelQuestion hsdModelQuestion){
		/**
		 * 得到父问题
		 */
		DBController.getInstance().getHsdModelQuestionObject(hsdModelQuestion.getSurveyuuid(), String.valueOf(hsdModelQuestion.getSortindex()), context, new CompleteHandler(){
			@Override
			void onComplete(Message msg) {
				// TODO Auto-generated method stub
				super.onComplete(msg);
				HsdModelQuestion hsdModelQuestion = (HsdModelQuestion) msg.obj;
				/**
				 * 得到子问题
				 */
				DBController.getInstance().getChildHsdModelQuestionObjec(hsdModelQuestion.getSurveyuuid(), hsdModelQuestion.getQuestionuuid(), context, new CompleteHandler(){
					@Override
					void onComplete(Message msg) {
						// TODO Auto-generated method stub
						super.onComplete(msg);
						/**
						 * 子问题list
						 */
						List<HsdModelQuestion> childHsdModelQuestionList = (List<HsdModelQuestion>) msg.obj;
						LayoutInflater inflater = (LayoutInflater)context.getSystemService
							      (Context.LAYOUT_INFLATER_SERVICE);
						for (HsdModelQuestion hsdModelQuestion : childHsdModelQuestionList) {
							LinearLayout linearLayout = (LinearLayout) inflater.inflate(R.layout.single_choice_item, null);
							TextView textView = (TextView) linearLayout.findViewById(R.id.single_c_tv);
							textView.setText(hsdModelQuestion.getDescription());
							final RadioGroup radioGroup = (RadioGroup) linearLayout.findViewById(R.id.single_c_radio);
							/**
							 * 得到每一个子问题对应的可供选答案
							 */
							DBController.getInstance().getHsdModelAnswerList(hsdModelQuestion.getFatherquestionuuid(), hsdModelQuestion.getSurveyuuid(), context, new CompleteHandler(){
								void onComplete(Message msg) {
									List<HsdModelAnswer> list = (List<HsdModelAnswer>) msg.obj;
									for (final HsdModelAnswer hsdModelAnswer : list) {
										System.out.println("得到每一个子问题对应的可供选答案"+hsdModelAnswer.getAnsweruuid());
										final RadioButton radioButton = new RadioButton(context);
//										radioButton.setBackgroundDrawable(getActivity().getResources().getDrawable(R.layout.radio_button_selector));
//										radioButton.setId(i);
										radioButton.setTag(hsdModelAnswer);
										radioButton.setText(hsdModelAnswer.getName());
										radioButton.setTextColor(Color.BLACK);
										radioGroup.addView(radioButton);
										
										/**
										 * 得到每一个子问题所对应的所选答案。
										 */
										DBController.getInstance().getHsdDataResultList(researchuuid,surveyuuid, hsdModelAnswer.getQuestionuuid(), context, new CompleteHandler(){
											void onComplete(Message msg) {
												List<HsdDataResult> list = (List<HsdDataResult>) msg.obj;
												if (list != null && list.size() > 0) {
													for (HsdDataResult hsdDataResult : list) {
														System.out.println("得到每一个子问题所对应的所选答案"+hsdModelAnswer.getAnsweruuid());
														if (hsdModelAnswer.getAnsweruuid().equals(hsdDataResult.getAnsweruuid())) {
										            		System.out.println("所选答案"+hsdModelAnswer.getAnsweruuid());
										            		radioButton.setChecked(true);
//										            		id = 0;
										            		return;
														}
													}
													
//													radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
//														
//														@Override
//														public void onCheckedChanged(RadioGroup group, int checkedId) {
//															try {
//																// TODO Auto-generated method stub
//																int id = group.getCheckedRadioButtonId();
//																RadioButton currentRadioButton = (RadioButton) view.findViewById(id);
//																
//																String answertext = currentRadioButton.getText().toString();
//																HsdModelAnswer hsdModelAnswer = (HsdModelAnswer) currentRadioButton.getTag();
////																System.out.println("updateQuestionResult----answertext---->"+hsdModelAnswer.getAnsweruuid()+answertext);
//																DBController.getInstance().updateQuestionResult(researchuuid, surveyuuid,  hsdModelAnswer.getAnsweruuid(), hsdModelAnswer.getQuestionuuid(),answertext, context,dealAnswerResultHandler);
//															} catch (Exception e) {
//																// TODO: handle exception
//																System.out.println("radioGroup.setOnCheckedChangeListener--excep----"+e.getMessage());
//															}
//															
//														}
//													});
												}else {
//													radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
//														
//														@Override
//														public void onCheckedChanged(RadioGroup group, int checkedId) {
//															// TODO Auto-generated method stub
//															int id = group.getCheckedRadioButtonId();
//															RadioButton currentRadioButton = (RadioButton) view.findViewById(id);
//															String answertext = currentRadioButton.getText().toString();
//															HsdModelAnswer hsdModelAnswer = (HsdModelAnswer) currentRadioButton.getTag();
//															System.out.println("insertQuestionResult---->"+hsdModelAnswer.getAnsweruuid());
//															DBController.getInstance().insertQuestionResult(researchuuid, surveyuuid, hsdModelAnswer.getAnsweruuid(), hsdModelAnswer.getQuestionuuid(),answertext, context,dealAnswerResultHandler);
//														}
//													});
												}
											};
										});
									}
								};
							});
							answerParentLayout.addView(linearLayout);
						}
						
						
						
					}
				});
				
			}
		});
	}
	
	@Override
	public boolean isHasResult() {
		// TODO Auto-generated method stub
		return super.isHasResult();
		
	}
	
	class CompleteHandler extends Handler{
		@Override
		public void handleMessage(Message msg) {
			// TODO Auto-generated method stub
			super.handleMessage(msg);
			onComplete(msg);
			
		}
		
		void onComplete(Message msg){
			
		}
	}
}
